﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BO;
using BL;

namespace AnimalFarm.Field_Building_manage
{
    public partial class AddNewBuilding : Form
    {
        public AddNewBuilding()
        {
            InitializeComponent();
            loadField();
        }

        private void loadField()
        {
            B_Field bf = B_Field.CreateInstance();
            DataTable dTable = bf.GetAllField();
            if (dTable != null)
            {
                ddlField.DataSource = dTable;
                ddlField.DisplayMember = "FieldName";
                ddlField.ValueMember = "FieldID";
            }
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            string buildingName = txtBuildingName.Text;
            string description = txtDescription.Text;
            int fieldID = int.Parse(ddlField.SelectedValue.ToString());

            Building_Info bi = new Building_Info();
            B_Building bb = B_Building.CreateInstance();

            if (fieldID > 0 && buildingName != null)
            {
                bi.BuildingName = buildingName;
                bi.FieldLocationID = fieldID;
                bi.Description = description;

                if (bi != null)
                {
                    bool isEffected = bb.CreateOneBuilding(bi);
                    if (isEffected == true)
                    {
                        MessageBox.Show("Create a new building successfully!", "Creating successuflly", MessageBoxButtons.OK);
                        txtBuildingName.Text = "";
                        txtBuildingName.Focus();
                    }
                    else
                    {
                        MessageBox.Show("Cannot create a new building successfully!", "Missing Data", MessageBoxButtons.OK);
                        txtBuildingName.Focus();
                    }
                }
            }
        }
    }
}
